package net.jarang.android.saleoffline.domain.appinfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class AppInfoDBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "test01_saleoffline.db";
    private static final int VERSION = 2;
    private static final String TABLE_NAME = "tb_app_info";
    private static final String COLUMN_ID = "my_phone_no";
    private static final String COLUMN_MEMBER_YN = "member_yn";
    private static final String COLUMN_LOGIN_YN = "login_yn";

    public AppInfoDBHelper(Context context) {
    	super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	String query = "CREATE TABLE " + TABLE_NAME + " (" +
        COLUMN_ID + " varchar(20) primary key, " +
        COLUMN_MEMBER_YN + " varchar(8) not null, " +
        COLUMN_LOGIN_YN + " varchar(8) not null )";
    	getWritableDatabase().execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    
    
    public boolean insert(Member appInfo) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_ID, appInfo.getPhoneNo());
        contentValues.put(COLUMN_MEMBER_YN, appInfo.getMemberYn());
        contentValues.put(COLUMN_LOGIN_YN, appInfo.getLoginYn());
        return getWritableDatabase().insert(TABLE_NAME, null, contentValues) != -1;
    }

    public Member select(String myPhoneNo) {
    	Member appInfo = null; 
    	Cursor cursor = getWritableDatabase().rawQuery("select my_phone_no, member_yn, login_yn from tb_app_info where my_phone_no=?", new String[]{myPhoneNo});
    	
    	Log.v("SALEOFFLINE", "selected : " + cursor.getCount());

    	if (cursor.moveToNext()) {
    		appInfo = new Member();
            appInfo.setPhoneNo(cursor.getString(0));
            appInfo.setMemberYn((cursor.getInt(1) == 0)? false : true);
            appInfo.setLoginYn((cursor.getInt(2) == 0)? false : true);
        }
        cursor.close();

        return appInfo;
    }

    public boolean updateMember(String myPhoneNo, boolean isMember) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_MEMBER_YN, isMember);
        String[] params = new String[] {myPhoneNo};
        int result = getWritableDatabase().update(TABLE_NAME, contentValues, COLUMN_ID + "=?", params);
        return result > 0;
    }

    public boolean updateLogin(String myPhoneNo, boolean isLogin) {
    	ContentValues contentValues = new ContentValues();
    	contentValues.put(COLUMN_LOGIN_YN, isLogin);
    	String[] params = new String[] {myPhoneNo};
    	int result = getWritableDatabase().update(TABLE_NAME, contentValues, COLUMN_ID + "=?", params);
    	return result > 0;
    }

    public boolean delete(String myPhoneNo) {
        String[] params = new String[] { myPhoneNo };
        int result = getWritableDatabase().delete(TABLE_NAME, COLUMN_ID + "=?", params);
        return result > 0;
    }
}

